home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Codecs / Std Compression Examples / Example3.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  8.7 KB  |  300 lines  |  [TEXT/MPS ]

  1. /*
  2.   File:            Example3.c
  3.   Contains:        Compression of PICT Files
  4.   Written by:    DTS and QT Engineering
  5.   Copyright:    © 1992-1994 by Apple Computer, Inc., all rights reserved.
  6.   Change History (most recent first):
  7.   <1>         12/4/94    khs        changed the format of the file to the new look and feel
  8.   To Do:
  9. */
  10.  
  11. //    The following sample code is again similar to the previous example except
  12. //    that it better deals with custom color tables, and demonstrates the use
  13. //    of extended procs and saving/restoring compression settings state.
  14.  
  15.  
  16. // INCLUDE FILES
  17. #include <menus.h>
  18. #include <fonts.h>
  19. #include <osevents.h>
  20. #include <components.h>
  21. #include <quicktimecomponents.h>
  22.  
  23.  
  24. // FUNCTION PROTOTYPES
  25. pascal Boolean Example3FilterProc(DialogPtr theDialog,
  26.                                   EventRecord* theEvent,
  27.                                   short* itemHit,
  28.                                   long refcon);
  29. pascal short Example3HookProc(DialogPtr theDialog,
  30.                               short itemHit,
  31.                               void* params,
  32.                               long refcon);
  33. void Example3(void);
  34.  
  35.  
  36. // FUNCTIONS
  37. void Example3(void)
  38. {
  39.     ComponentResult result;
  40.     Point where;
  41.     ComponentInstance ci;
  42.     short sref;
  43.     SFTypeList typeList;
  44.     SFReply reply;
  45.     short firstFile = true;
  46.  
  47.     //    Open the Standard Compression Dialog component.
  48.  
  49.     ci = OpenDefaultComponent(StandardCompressionType, StandardCompressionSubType);
  50.     if (ci)
  51.     {
  52.  
  53.         //    Tell SFGetFilePreview to center on the best monitor.
  54.  
  55.         where.h = where.v = -2;
  56.  
  57.         //    Show only 'PICT' files in the file list.
  58.  
  59.         typeList[0] = 'PICT';
  60.  
  61.         //    Keep asking the user for files until they cancel.
  62.  
  63.         reply.good = true;
  64.         while (reply.good)
  65.         {
  66.  
  67.             //    Ask user to select PICT file to be compressed.
  68.  
  69.             SFGetFilePreview(where, "\p", nil, 1, typeList, nil, &reply);
  70.             if (reply.good)
  71.             {
  72.  
  73.                 //    If they selected a file, open that file.
  74.  
  75.                 result = FSOpen(reply.fName, reply.vRefNum, &sref);
  76.                 if (!result)
  77.                 {
  78.  
  79.                     //    If the file opened successfully, set the picture file to
  80.                     //    be the test image shown in the dialog.  Passing nil for srcRect
  81.                     //    means use the entire image.  Passing 0 for testFlags means
  82.                     //    to use the default system method of displaying the test image
  83.                     //    which is currently a combination of cropping and scaling.
  84.  
  85.                     SCSetTestImagePictFile(ci, sref, nil, 0);
  86.  
  87.                     //    If this is the first picture file being compressed do one-time
  88.                     //    setup of defaults and extended procs described below.
  89.  
  90.                     if (firstFile)
  91.                     {
  92.  
  93.                         //    Select defaults for the picture file.  Subsequent
  94.                         //    picture files will only have defaults chosen for
  95.                         //    them if the user presses the custom “Defaults” button
  96.                         //    in the compression dialog.
  97.  
  98.                         result = SCDefaultPictFileSettings(ci, sref, false);
  99.                         if (!result)
  100.                         {
  101.  
  102.                             //    Prepare and set the extended procs to be used by the
  103.                             //    compression dialog.  The filterProc is used to handle
  104.                             //    any events which the compression dialog doesn't know about.
  105.                             //    for
  106.  
  107.                             SCExtendedProcs xprocs;
  108.  
  109.                             //    The filterProc is used to handle any events which the
  110.                             //    compression dialog doesn't know about.  For example,
  111.                             //    the filterProc would handle update events for application
  112.                             //    windows uncovered by the movable modal compression dialog.
  113.                             //    The previous two examples don't supply a filterProc because
  114.                             //    they are not full applications with other windows and a menu
  115.                             //    bar.  However, all applications using the compression dialog
  116.                             //    should provide a filterProc to handle updates for their windows.
  117.                             //
  118.                             //    It is also possible to use the filterProc for update handling, etc.
  119.                             //    without using the custom button. Set the hookProc to nil and
  120.                             //    customName to "\p" and the custom button will not be displayed.
  121.  
  122.                             xprocs.filterProc = Example3FilterProc;
  123.  
  124.                             //    The hookProc handles clicks in the custom button and responds
  125.                             //    accordingly.  In our example, it will change the current settings
  126.                             //    to the defaults for the test image.
  127.  
  128.                             xprocs.hookProc = Example3HookProc;
  129.  
  130.                             //    In this example, we pass the component instance of the
  131.                             //    compression dialog in as a refcon.  This value will then
  132.                             //    be available in our filter and hook procs to access the
  133.                             //    the compression dialog component.  This refcon could be
  134.                             //    any value that is useful to the filter and/or hook procs.
  135.  
  136.                             xprocs.refcon = (long)ci;
  137.  
  138.                             //    Copy the string for our custom button into the extended
  139.                             //    procs structure.
  140.  
  141.                             BlockMove("\pDefaults", xprocs.customName, 9);
  142.  
  143.                             //    Set the current extended procs to be our procs.
  144.                             //
  145.                             //    We could clear out the extended procs at any time
  146.                             //    by calling SCSetInfo(ci,scExtendedProcsType,nil);
  147.  
  148.                             result = SCSetInfo(ci, scExtendedProcsType, &xprocs);
  149.                         }
  150.  
  151.                         firstFile = false;
  152.                     }
  153.                     else
  154.                     {
  155.  
  156.                         //    If this is not the first pass, we probably want to clear out
  157.                         //    the color table chosen as the default in the first pass because
  158.                         //    it's most likely not appropriate for the current picture.  We could
  159.                         //    do this by calling SCSetInfo(ci,scColorTableType,nil) which would
  160.                         //    remove any custom color table completely.  The picture would then
  161.                         //    be compressed using the standard Macintosh color tables.
  162.                         //
  163.                         //    A better thing to do would be to keep the current defaults but
  164.                         //    set a new color table appropriate for the picture.  Unfortunately,
  165.                         //    there isn't a call to just get the default color table from the
  166.                         //    picture file.  That means saving the current settings, getting the
  167.                         //    default settings including a new color table, then restoring
  168.                         //    the previous settings while leaving the new color table.
  169.  
  170.                         Handle h;
  171.  
  172.                         //    Get a handle containing the current spatial compression settings.
  173.                         //    If we were dealing with sequences of images, this handle would also
  174.                         //    contain temporal and data rate settings.  The handle only includes
  175.                         //    information useful across reboots.  It does not include information
  176.                         //    such as the current test image, color table, window position, etc.
  177.                         //    The handle is created for us but we must dispose it.
  178.  
  179.                         result = SCGetInfo(ci, scSettingsStateType, &h);
  180.                         if (!result)
  181.                         {
  182.  
  183.                             //    Get default settings based on the picture file we are compressing.
  184.                             //    This includes the color table we will be interested in.
  185.  
  186.                             result = SCDefaultPictFileSettings(ci, sref, false);
  187.                             if (!result)
  188.  
  189.                             //    Restore the compression settings while leaving the color
  190.                             //    table set to the one from the current picture file.
  191.  
  192.                                 result = SCSetInfo(ci, scSettingsStateType, &h);
  193.  
  194.                             //    We have to throw away the handle created by SCGetInfo.
  195.  
  196.                             DisposeHandle(h);
  197.                         }
  198.                     }
  199.  
  200.                     //    If no errors have occured continue.
  201.  
  202.                     if (!result)
  203.                     {
  204.  
  205.                         //    Display the compress dialog to the user and request compression
  206.                         //    settings from them.  Note that the result code returned could
  207.                         //    include scUserCancelled.
  208.  
  209.                         result = SCRequestImageSettings(ci);
  210.                         if (!result)
  211.                         {
  212.  
  213.                             //    Compress the picture file with the settings chosen by the user.
  214.                             //    The settings include any custom color table found in the source
  215.                             //    picture if still appropriate for the depth chosen by the user.
  216.                             //
  217.                             //    Again note that we are able to pass the source file ref for both the
  218.                             //    source and destination picture files.  In this case, the picture
  219.                             //    file will be compressed in place.  It would probably be better to
  220.                             //    ask the user for a name to save the compressed file as, rather than
  221.                             //    compressing it in place.
  222.  
  223.                             result = SCCompressPictureFile(ci, sref, sref);
  224.                         }
  225.                     }
  226.  
  227.                     //    Close the source picture file.
  228.  
  229.                     FSClose(sref);
  230.                 }
  231.             }
  232.         }
  233.  
  234.         //    Close the Standard Compression Dialog component.
  235.  
  236.         CloseComponent(ci);
  237.     }
  238. }
  239.  
  240.  
  241. pascal Boolean Example3FilterProc(DialogPtr theDialog,
  242.                                   EventRecord* theEvent,
  243.                                   short* itemHit,
  244.                                   long refcon)
  245. {
  246. #pragma unused (itemHit, refcon)
  247.  
  248.     if (theEvent->what == updateEvt)
  249.     {
  250.         if ((WindowPtr)theEvent->message != theDialog)
  251.         {
  252.             // handle updates for application windows
  253.         }
  254.     }
  255.     return (false);
  256. }
  257.  
  258.  
  259. pascal short Example3HookProc(DialogPtr theDialog,
  260.                               short itemHit,
  261.                               void* params,
  262.                               long refcon)
  263. {
  264. #pragma unused (theDialog)
  265.  
  266.     //    Check to see if the item clicked in is our custom button.
  267.  
  268.     if (itemHit == scCustomItem)
  269.     {
  270.  
  271.         //    Let Standard Compression select some defaults for our picture file.
  272.  
  273.         SCDefaultPictFileSettings(params, *(short*)refcon, false);
  274.  
  275.         //    Note that we could set test image in here if we wanted.
  276.     }
  277.  
  278.     //    Always return the itemHit.
  279.  
  280.     return (itemHit);
  281. }
  282.  
  283.  
  284. // MAIN FUNCTION
  285. void main(void)
  286. {
  287.     InitGraf(&qd.thePort);
  288.     InitFonts();
  289.     FlushEvents(everyEvent, 0);
  290.     InitWindows();
  291.     InitMenus();
  292.     InitDialogs(nil);
  293.     InitCursor();
  294.     MaxApplZone();
  295.  
  296.     Example3();
  297. }
  298.  
  299.  
  300.